/*
Copyright (C) 2010  Christoph Schnack, Berlin (Germany)

This file is part of Tribuit.

    Tribuit is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Tribuit is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Tribuit.  If not, see <http://www.gnu.org/licenses/>.
*/
package OtherDialogs;

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;
import GuiTools.*;
import Mainframe.*;
import Settings.*;
import Storage.Storage;

public class OptionDialog extends TribuitFrame{
	JComboBox languageCB,lafCB,presetsCB;
	PJCheckBox autosaveCB,soundOnCB,addToTrayCB;
	PJRadioButton saveInLines,saveInStream,selectionRB,themeRB;
	RGBSpinnerPanel bgSpinnerPanel;
	JSlider volumeSlider;
	Settings SET;
	TribuitFrame optionFrame;
	Mainframe MAINFRAME;
	Storage MAINVOC;
	Language LANG;
	Color BGCOLOR = Color.lightGray;
	HashMap<String,String> lafsHM = new HashMap<String,String>();
	TribuitMenu MENU;
	
	public OptionDialog(Storage sto,Settings set,Mainframe frame,TribuitMenu menu){
		super("Optionen");
		optionFrame=this;
		MENU=menu;
		MAINFRAME = frame;
		LANG = set.getLanguage();
		SET = set;
		MAINVOC = sto;
		optionFrame.setTitle(LANG.getSettings());
		languageCB = new JComboBox();
		File dir = new File(GuiTools.TRIBUITHOME+File.separator+"settings");
		String[] fileList = dir.list(new FilenameFilter() {
		    public boolean accept(File d, String name) {
		       return name.endsWith(".lang");
		    }
		});
		
		for(int i=0;i<fileList.length;i++){
			String tmp = fileList[i];
			String ad = tmp.replaceAll(".lang", "");
			languageCB.addItem(ad);
		}
		languageCB.setSelectedItem(set.getCurrentLanguage());
		JPanel languageLP = new JPanel();
		languageLP.setBackground(BGCOLOR);
		languageLP.setBorder(BorderFactory.createTitledBorder(LANG.getOdLanguage()));
		languageLP.add(languageCB);
		
		autosaveCB = new PJCheckBox(LANG.getActivate(),SET.getAutosave());
		JLabel aseLabel = new JLabel(LANG.getAutosaveExplanation());
		aseLabel.setFont(new Font(Font.SANS_SERIF,Font.ITALIC,10));
		JPanel autosaveLP = new JPanel();
		autosaveLP.setLayout(new GridLayout(2,1));
		autosaveLP.setBackground(BGCOLOR);
		autosaveLP.setBorder(BorderFactory.createTitledBorder(LANG.getOdAutosave()));
		autosaveLP.add(aseLabel);
		autosaveLP.add(autosaveCB);
		
		soundOnCB = new PJCheckBox(LANG.getActivate(),SET.isSoundOn());
		soundOnCB.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				volumeSlider.setEnabled(soundOnCB.isSelected());
			}
		});
		volumeSlider = new JSlider(0,100,SET.getVolume());
		volumeSlider.setPaintTicks(true);
		volumeSlider.setPaintLabels(true);
		volumeSlider.setMinorTickSpacing(5);
		volumeSlider.setMajorTickSpacing(20);
		volumeSlider.setEnabled(SET.isSoundOn());
		volumeSlider.setOpaque(false);
		JPanel soundPanel = new JPanel();
		soundPanel.setOpaque(false);
		soundPanel.setLayout(new GridLayout(3,1));
		soundPanel.add(soundOnCB);
		soundPanel.add(new JLabel(LANG.getVolume()+":"));
		soundPanel.add(volumeSlider);
		JPanel soundLP = new JPanel();
		soundLP.setBackground(BGCOLOR);
		soundLP.setBorder(BorderFactory.createTitledBorder(LANG.getSound()));
		soundLP.add(soundPanel);
		
		addToTrayCB = new PJCheckBox(LANG.getActivate(),SET.getAddToSystemTray());
		addToTrayCB.setEnabled(SystemTray.isSupported());
		JPanel addToTrayPanel = new JPanel();
		addToTrayPanel.setBackground(BGCOLOR);
		addToTrayPanel.setBorder(BorderFactory.createTitledBorder(LANG.getAddToTray()));
		addToTrayPanel.add(addToTrayCB);
		
		Vector<String> lafs = new Vector<String>();
		for(UIManager.LookAndFeelInfo laf : UIManager.getInstalledLookAndFeels()){
			lafsHM.put(laf.getName(),laf.getClassName());
			lafs.add(laf.getName());
		}
		lafCB = new JComboBox(lafs);
		lafCB.setSelectedItem(UIManager.getLookAndFeel().getName());
		lafCB.addPopupMenuListener(new PopupMenuListener(){
			public void popupMenuCanceled(PopupMenuEvent arg) {}

			public void popupMenuWillBecomeInvisible(PopupMenuEvent arg) {}

			public void popupMenuWillBecomeVisible(PopupMenuEvent arg) {
				if((MENU.WORDCHECKRUNNING || MENU.WORDTABLERUNNING || ((MENU.VMDM!=null)?MENU.VMDM.isRunning():false)) || ((MENU.PMDM==null)?false:MENU.PMDM.isRunning()) || ((MENU.PIDM==null)?false:MENU.PIDM.isRunning()) || ((MENU.CONJUGATEDIALOG!=null)?!MENU.CONJUGATEDIALOG.isDead():false) || ((MENU.MANUAL!=null)?MENU.MANUAL.manualFrame.isDisplayable():false) || ((MENU.INFOFRAME!=null)?MENU.INFOFRAME.isDisplayable():false) || ((MENU.CHARTABLE!=null)?MENU.CHARTABLE.isDisplayable():false)){ 
					final JComboBox cb = (JComboBox) arg.getSource();
					SwingUtilities.invokeLater(new Runnable(){
						public void run(){
							cb.validate();
							JOptionPane.showMessageDialog(optionFrame,LANG.getCloseAllWindows(),LANG.getAttention(), JOptionPane.WARNING_MESSAGE);
						}
					});
				}
			}
		});
		JPanel lafLP = new JPanel();
		lafLP.setBackground(BGCOLOR);
		lafLP.setBorder(BorderFactory.createTitledBorder("LookAndFeel"));
		lafLP.add(lafCB);
		
		
		final JButton tribuitColorButton = new JButton("edit");
		tribuitColorButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				new ChooseTribuitColors();
			}
		});
		JPanel colorsPanel = new JPanel();
		colorsPanel.setLayout(new GridBagLayout());
		GridBagConstraints cGBC = new GridBagConstraints();
		cGBC.anchor = GridBagConstraints.NORTHWEST;
		cGBC.fill = GridBagConstraints.BOTH;
		cGBC.weightx=1;
		bgSpinnerPanel = new RGBSpinnerPanel(SET.getBackgroundColor(),SET.getBgColorRandom());
		bgSpinnerPanel.setTheBackground(BGCOLOR);
		
		presetsCB = new JComboBox();
		presetsCB.addItem("default");
		presetsCB.addItem("childish");
		presetsCB.addItem("jazzy");
		presetsCB.addItem("gray");
		if(!SET.getColorTheme().equals("none")) presetsCB.setSelectedItem(SET.getColorTheme());
		themeRB = new PJRadioButton("theme");
		themeRB.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				presetsCB.setEnabled(true);
				tribuitColorButton.setEnabled(false);
				bgSpinnerPanel.setEnabled(false);
			}
		});
		selectionRB = new PJRadioButton("selection");
		selectionRB.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				presetsCB.setEnabled(false);
				tribuitColorButton.setEnabled(true);
				bgSpinnerPanel.setEnabled(true);
			}
		});
		ButtonGroup colBG = new ButtonGroup();
		colBG.add(themeRB);
		colBG.add(selectionRB);
		JPanel themePanel = new JPanel(new GridBagLayout());
		GridBagConstraints g = new GridBagConstraints();
		g.anchor = GridBagConstraints.NORTHWEST;
		g.weightx=1;
		themePanel.add(themeRB,g);
		themePanel.add(presetsCB,g);
		themePanel.setOpaque(false);
		if(!SET.getColorTheme().equals("none"))themeRB.doClick();
		else selectionRB.doClick();
		
		JLabel bgLabel =new JLabel(LANG.getBackground()+":");
		bgLabel.setOpaque(false);
		bgLabel.setFont(new Font(Font.SERIF,Font.PLAIN,14));
		JLabel tribuitLabel =new JLabel("TribuitLogo:");
		tribuitLabel.setOpaque(false);
		tribuitLabel.setFont(new Font(Font.SERIF,Font.PLAIN,14));
		colorsPanel.add(themePanel,cGBC);
		cGBC.gridy=1;
		colorsPanel.add(selectionRB,cGBC);
		cGBC.gridy=2;
		colorsPanel.add(bgLabel,cGBC);
		cGBC.gridy=3;
		colorsPanel.add(bgSpinnerPanel,cGBC);
		cGBC.gridy=4;
		colorsPanel.add(tribuitLabel,cGBC);
		cGBC.fill = GridBagConstraints.NONE;
		cGBC.gridy=5;
		colorsPanel.add(tribuitColorButton,cGBC);
		colorsPanel.setBackground(BGCOLOR);
		colorsPanel.setBorder(BorderFactory.createTitledBorder(LANG.getColorSelection()));
		
		saveInLines = new PJRadioButton(LANG.getWriteInLines());
		saveInStream = new PJRadioButton(LANG.getWriteInStream());
		JLabel wilTA = new JLabel(LANG.getSaveInLinesExplanation());
		wilTA.setFont(new Font(Font.SANS_SERIF,Font.ITALIC,10));
		if(SET.getWriteInLines()) saveInLines.setSelected(true);
		else saveInStream.setSelected(true);
		ButtonGroup bg = new ButtonGroup();
		bg.add(saveInLines);
		bg.add(saveInStream);
		JPanel savingMode = new JPanel();
		savingMode.setBackground(BGCOLOR);
		savingMode.setLayout(new GridLayout(3,1));
		savingMode.setBorder(BorderFactory.createTitledBorder(LANG.getSaveMode()));
		savingMode.add(wilTA);
		savingMode.add(saveInStream);
		savingMode.add(saveInLines);
		
		int jspX=300,jspY=300;
		JTabbedPane tabs = new JTabbedPane();
		JPanel programPanel = new JPanel();
		programPanel.setLayout(new GridBagLayout());
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.ipadx = 10;
		gbc.ipady = 10;
		gbc.weightx=2;
		gbc.weighty=2;
		gbc.insets = new Insets(5,5,5,5);
		gbc.anchor = GridBagConstraints.NORTHWEST;
		gbc.fill = GridBagConstraints.BOTH;
		gbc.gridx=1;
		gbc.gridy=1;
		programPanel.add(soundLP,gbc);
		JScrollPane programJSP = new JScrollPane(programPanel);
		programJSP.setPreferredSize(new Dimension(jspX,jspY));
		programJSP.getVerticalScrollBar().setUnitIncrement(30);
		tabs.addTab(LANG.getProgram(), programJSP);
		
		JPanel GUIPanel = new JPanel();
		GUIPanel.setLayout(new GridBagLayout());
		gbc.gridx=1;
		gbc.gridy=1;
		GUIPanel.add(languageLP,gbc);
		gbc.gridy=2;
		gbc.gridwidth=2;
		GUIPanel.add(addToTrayPanel,gbc);
		gbc.gridwidth=1;
		gbc.gridy=3;
		GUIPanel.add(lafLP,gbc);
		gbc.gridy=4;
		GUIPanel.add(colorsPanel,gbc);
		JScrollPane guiJSP = new JScrollPane(GUIPanel);
		guiJSP.setPreferredSize(new Dimension(jspX,jspY));
		guiJSP.getVerticalScrollBar().setUnitIncrement(30);
		tabs.addTab("GUI",guiJSP);
		
		JLabel warningLabel = new JLabel(LANG.getSaveWarning());
		warningLabel.setFont(new Font(Font.SANS_SERIF,Font.ITALIC,10));
		LabelPanel warningLP = new LabelPanel(LANG.getAttention(),warningLabel);
		warningLP.setBackground(Color.CYAN);
		
		JPanel savePanel = new JPanel();
		savePanel.setLayout(new GridBagLayout());
		gbc.gridx=1;
		gbc.gridy=1;
		savePanel.add(warningLP,gbc);
		gbc.gridy=2;
		savePanel.add(autosaveLP,gbc);
		gbc.gridy=3;
		savePanel.add(savingMode,gbc);
		JScrollPane saveJSP = new JScrollPane(savePanel);
		saveJSP.setPreferredSize(new Dimension(jspX,jspY));
		saveJSP.getVerticalScrollBar().setUnitIncrement(30);
		tabs.addTab(LANG.getSaving(),saveJSP);
		
		JPanel buttonPanel = new JPanel();
		buttonPanel.setBackground(GuiTools.BGCOLOR);
		JButton confirm = new JButton(LANG.getOdConfirm());
		confirm.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				boolean save=false;
				if(SET.getWriteInLines()!=saveInLines.isSelected() || (!SET.getAutosave()&&autosaveCB.isSelected()) && MAINVOC.isChanged()){
					save=true;
					int ans = JOptionPane.showOptionDialog(optionFrame, LANG.getSaveReally(), LANG.getAttention(), JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE, null, new String[]{LANG.getSaveAndContinue(),LANG.getBack(),LANG.getReseteSaveOptionsAndGoOn()}, LANG.getSaveAndContinue());
					if(ans == JOptionPane.NO_OPTION) return;
					if(ans == JOptionPane.CANCEL_OPTION){
						autosaveCB.setSelected(SET.getAutosave());
						saveInLines.setSelected(SET.getWriteInLines());
						save = false;
					}
				}
				SET.setLanguage((String)languageCB.getSelectedItem());
				SET.setAutosave(autosaveCB.isSelected());
				SET.setSoundOn(soundOnCB.isSelected());
				SET.setVolume(volumeSlider.getValue());
				SET.setWriteInLines(saveInLines.isSelected());
				SET.setAddToSystemTray(addToTrayCB.isSelected());
				int red=bgSpinnerPanel.getRed(),green=bgSpinnerPanel.getGreen(),blue=bgSpinnerPanel.getBlue();
				if(selectionRB.isSelected()){
					SET.setBackgroundColor(red, green, blue);
					SET.setBgColorRandom(bgSpinnerPanel.isRandomColor());
					SET.setColorTheme("none");
				}
				else getTheme();
				if(MAINFRAME.getMenu().getVMDM()!=null && MAINFRAME.getMenu().getVMDM().getCurrentDialog().getName().equals("evdFrame")){
					MAINFRAME.getMenu().getVMDM().getCurrentDialog().updateMenuBar();
				}
				SET.setLookAndFeel(lafsHM.get((String)lafCB.getSelectedItem()));
				if(!SET.save()) JOptionPane.showMessageDialog(optionFrame, LANG.getWarningCannotWriteSettingsFile(),LANG.getAttention(),JOptionPane.WARNING_MESSAGE);
				if(save)MAINVOC.saveAllVocabularies();
				optionFrame.dispose();
				
				MAINFRAME.updateMainframe();
			}
		});
		JButton cancel = new JButton(LANG.getCancel());
		cancel.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				optionFrame.dispose();
			}
		});
		buttonPanel.add(confirm);
		buttonPanel.add(cancel);
		optionFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		optionFrame.addPanel("north", new Header(LANG.getSettings(),Color.white,new Color(255,126,0),30,new Font(Font.SERIF,Font.BOLD,30)));
		optionFrame.addPanel("center", tabs);
		optionFrame.addPanel("south", buttonPanel);
		optionFrame.pack();
		optionFrame.showUpAt(frame);
	}
	
	public void getTheme(){
		String selection = (String)presetsCB.getSelectedItem();
		String[] colors=GuiTools.LOGO_DEFAULT_COLORS.split("%"),bgCol=GuiTools.BG_BLACK_COLOR.split(",");;
		if(selection.equals("childish")){
			colors = GuiTools.LOGO_TOY_COLORS.split("%");
			bgCol = GuiTools.BG_TOY_COLOR.split(",");
		}
		if(selection.equals("jazzy")){
			colors = GuiTools.LOGO_COLOURED_COLORS.split("%");
		}	
		if(selection.equals("gray")){
			colors = GuiTools.LOGO_GRAY_COLORS.split("%");
			bgCol = GuiTools.BG_GRAY_COLOR.split(",");
		}	
		Color[] cols = new Color[7];
		int i = 0;
		for(String s: colors){
			String[] c = s.split(",");
			cols[i++] = new Color(Integer.parseInt(c[0]),Integer.parseInt(c[1]),Integer.parseInt(c[2]));
		}
		boolean[] bools = new boolean[7];
		SET.setColorTheme(selection);
		SET.setLogoColorsRandom(bools);
		SET.setLogoColors(cols); 
		SET.setBackgroundColor(Integer.parseInt(bgCol[0]), Integer.parseInt(bgCol[1]), Integer.parseInt(bgCol[2]));
	}
	
	class ChooseTribuitColors extends JDialog{
		JDialog dialog;
		RGBSpinnerPanel[] RGBPanels = new RGBSpinnerPanel[7];
		RGBSpinnerPanel allToRGBPanel;
		JPanel spinnerPanels,displayPanel;
		JRadioButton allToRB,selectionRB;
		public ChooseTribuitColors(){
			super(optionFrame,"TribuitColors",true);
			dialog=this;
			selectionRB = new JRadioButton(LANG.getSelect());allToRB = new JRadioButton(LANG.getSetAllTo());
			allToRGBPanel = new RGBSpinnerPanel(new Color(255,255,255),false);
			spinnerPanels = new JPanel();
			String[] tribuit = {"T","R","I","B","U","I","T"};
			Color[] colors = SET.getLogoColors();
			boolean[] bools = SET.getLogoColorsRandom();
			for(int i=0;i<7;i++){
				RGBPanels[i] = new RGBSpinnerPanel(colors[i],bools[i]);
				LabelPanel tmp = new LabelPanel(tribuit[i],RGBPanels[i]);
				tmp.label.setFont(new Font(Font.SERIF,Font.BOLD,18));
				spinnerPanels.add(tmp);
			}
			displayPanel = new JPanel();
			selectionRB.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent e){
					displayPanel.removeAll();
					displayPanel.add(spinnerPanels);
					dialog.pack();
				}
			});
			allToRB.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent e){
					displayPanel.removeAll();
					displayPanel.add(allToRGBPanel);
					dialog.pack();
				}
			});
			ButtonGroup bg = new ButtonGroup();
			bg.add(selectionRB);bg.add(allToRB);
			JButton okBtn = new JButton("ok");
			okBtn.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent e){
					boolean[] bool = new boolean[7];
					Color[] cols = new Color[7];
					for(int i=0;i<7;i++){
						if(selectionRB.isSelected()){
							RGBSpinnerPanel sp = RGBPanels[i];
							cols[i]= sp.getColor();
							bool[i]=sp.isRandomColor();
						}
						if(allToRB.isSelected()){
							cols[i]=allToRGBPanel.getColor();
							bool[i]=allToRGBPanel.isRandomColor();
						}
					}
					SET.setLogoColors(cols);
					SET.setLogoColorsRandom(bool);
					SET.save();
					MAINFRAME.updateMainframe();
					dialog.dispose();
				}
			});
			JButton cancelBtn = new JButton(LANG.getCancel());
			cancelBtn.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent e){
					dialog.dispose();
				}
			});
			JPanel buttonPanel = new JPanel();
			buttonPanel.setBackground(GuiTools.BGCOLOR);
			buttonPanel.add(okBtn);buttonPanel.add(cancelBtn);
			
			spinnerPanels.setBorder(BorderFactory.createLineBorder(Color.black,2));
			
			JPanel rbPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
			rbPanel.add(selectionRB);rbPanel.add(allToRB);
			JPanel centerPanel = new JPanel();
			centerPanel.setLayout(new GridBagLayout());
			GridBagConstraints gbc = new GridBagConstraints();
			gbc.anchor = GridBagConstraints.NORTHWEST;
			gbc.fill = GridBagConstraints.BOTH;
			gbc.weighty=2;
			gbc.weightx=2;
			centerPanel.add(rbPanel,gbc);
			gbc.gridy=1;
			gbc.weighty=1;
			centerPanel.add(displayPanel,gbc);
			JScrollPane jsp= new JScrollPane(centerPanel);
			jsp.setPreferredSize(new Dimension(400,210));
			dialog.getContentPane().add(BorderLayout.NORTH, new Header("Logo Colors"));
			dialog.getContentPane().add(BorderLayout.CENTER,jsp);
			dialog.getContentPane().add(BorderLayout.SOUTH,buttonPanel);
			dialog.pack();
			dialog.setLocation(optionFrame.getLocation());
			SwingUtilities.invokeLater(new Runnable(){
				public void run(){
					selectionRB.doClick();
				}
			});
			dialog.setVisible(true);
		}
	}
	
	class RGBSpinnerPanel extends JPanel{
		JSpinner redSpinner,greenSpinner,blueSpinner;
		JButton chooseButton;
		JRadioButton randomRB,choiceRB;
		JPanel rbPanel,spinnerPanel;
		boolean randomColor=false;
		
		public RGBSpinnerPanel(Color defaultColor, boolean random){
			SpinnerModel redModel = new SpinnerNumberModel(defaultColor.getRed()+1,1,256,1);
			SpinnerModel greenModel = new SpinnerNumberModel(defaultColor.getGreen()+1,1,256,1);
			SpinnerModel blueModel = new SpinnerNumberModel(defaultColor.getBlue()+1,1,256,1);
			redSpinner= new JSpinner(redModel);
			redSpinner.setOpaque(false);
			greenSpinner= new JSpinner(greenModel);
			greenSpinner.setOpaque(false);
			blueSpinner= new JSpinner(blueModel);
			blueSpinner.setOpaque(false);
			chooseButton = new JButton(LANG.getColorTable());
			chooseButton.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent e){
					Color c = JColorChooser.showDialog(optionFrame, "Color", SET.getBackgroundColor());
					if(c!=null){
						redSpinner.setValue(c.getRed()+1);
						greenSpinner.setValue(c.getGreen()+1);
						blueSpinner.setValue(c.getBlue()+1);
					}
				}
			});
			spinnerPanel = new JPanel();
			spinnerPanel.add(new LabelPanel(LANG.getRed(),redSpinner));
			spinnerPanel.add(new LabelPanel(LANG.getGreen(),greenSpinner));
			spinnerPanel.add(new LabelPanel(LANG.getBlue(),blueSpinner));
			randomRB = new JRadioButton(LANG.getRandomly());
			randomRB.setOpaque(false);
			randomRB.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent e){
					redSpinner.setEnabled(false);
					greenSpinner.setEnabled(false);
					blueSpinner.setEnabled(false);
					chooseButton.setEnabled(false);
					randomColor=true;
				}
			});
			choiceRB = new JRadioButton(LANG.getSelect());
			choiceRB.setOpaque(false);
			choiceRB.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent e){
					redSpinner.setEnabled(true);
					greenSpinner.setEnabled(true);
					blueSpinner.setEnabled(true);
					chooseButton.setEnabled(true);
					randomColor=false;
				}
			});
			ButtonGroup bg = new ButtonGroup();
			bg.add(randomRB);
			bg.add(choiceRB);
			rbPanel = new JPanel();
			rbPanel.add(randomRB);
			rbPanel.add(choiceRB);
			this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
			this.add(rbPanel);;
			this.add(spinnerPanel);
			this.add(chooseButton);
			if(random)randomRB.doClick();
			else choiceRB.doClick();
		}
		
		public void setTheBackground(Color c){
			this.setBackground(c);
			rbPanel.setBackground(c);
			spinnerPanel.setBackground(c);
		}
		
		public void setEnabled(boolean enabled){
			redSpinner.setEnabled(enabled);
			greenSpinner.setEnabled(enabled);
			blueSpinner.setEnabled(enabled);
			chooseButton.setEnabled(enabled);
			choiceRB.setEnabled(enabled);
			randomRB.setEnabled(enabled);
		}
		public int getRed(){return (Integer) redSpinner.getValue()-1;}
		public int getGreen(){return (Integer) greenSpinner.getValue()-1;}
		public int getBlue(){return (Integer) blueSpinner.getValue()-1;}
		public Color getColor(){return new Color(getRed(),getGreen(),getBlue());}
		public boolean isRandomColor(){return randomColor;}
		
	}
}